﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cocbook_DAL;
using Cocbook_DTO;

namespace Cocbook_DAL
{
    public class Bill_BookDAL
    {
        public bool CreateBill_Book(Bill_Book bb)
        {
            DB.SetQuery("INSERT INTO Bill_Book VALUES(@billid, @bookid, @discount ,@copy)");
            DB.AddParameters("billid", bb.Bill.BillID);
            DB.AddParameters("bookid", bb.Book.BookID);
            DB.AddParameters("discount", bb.Discount);
            DB.AddParameters("copy", bb.Copy);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.CreateBill_Book", e.ToString());
                return false;
            }
        }

        public bool UpdateBill_Book(Bill_Book bb)
        {
            DB.SetQuery("UPDATE Bill_Book SET Discount = @discount, Copy = @copy WHERE (BillID = @billid) and (BookID = @bookid)");
            DB.AddParameters("billid", bb.Bill.BillID);
            DB.AddParameters("bookid", bb.Book.BookID);
            DB.AddParameters("discount", bb.Discount);
            DB.AddParameters("copy", bb.Copy);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.UpdateBill_Book", e.ToString());
                return false;
            }
        }

        public bool DeleteBill_BookByBillID(int billId)
        {
            DB.SetQuery("DELETE FROM Bill_Book WHERE (BillID = @billid)");
            DB.AddParameters("billid", billId);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.DeleteBill_Book", e.ToString());
                return false;
            }
        }

        public bool DeleteBill_Book(int billId, int bookId)
        {
            DB.SetQuery("DELETE FROM Bill_Book WHERE (BillID = @billid) and (BookID = @bookid)");
            DB.AddParameters("billid", billId);
            DB.AddParameters("bookid", bookId);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.DeleteBill_Book", e.ToString());
                return false;
            }
        }

        public List<Bill_Book> GetBill_BookByBillID(int id)
        {
            DB.SetQuery("SELECT BillID, BookID, Discount, Copy FROM Bill_Book WHERE BillID = @id");
            DB.AddParameters("id", id);

            try
            {
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Bill_Book> BB = new List<Bill_Book>();
                Bill_Book bb = new Bill_Book();
                if (llo.Count > 0)
                {
                    foreach (List<object> lo in llo)
                    {
                        bb = new Bill_Book();
                        bb.Bill = (new BillDAL()).GetBillByID(Convert.ToInt16(lo[0]));
                        bb.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[1]));
                        bb.Discount = Convert.ToDouble(lo[2]);
                        bb.Copy = Convert.ToInt16(lo[3]);
                        BB.Add(bb);
                    }
                }
                else "".Substring(2);
                return BB;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetBill_BookByBillID", e.ToString());
                return null;
            }
        }

        public List<Bill_Book> GetBill_BookByBookID(int id)
        {
            DB.SetQuery("SELECT BillID, BookID, Discount, Copy FROM Bill_Book WHERE BookID = @id");
            DB.AddParameters("id", id);

            try
            {
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Bill_Book> BB = new List<Bill_Book>();
                Bill_Book bb = new Bill_Book();
                if (llo.Count > 0)
                {
                    foreach (List<object> lo in llo)
                    {
                        bb = new Bill_Book();
                        bb.Bill = (new BillDAL()).GetBillByID(Convert.ToInt16(lo[0]));
                        bb.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[1]));
                        bb.Discount = Convert.ToDouble(lo[2]);
                        bb.Copy = Convert.ToInt16(lo[3]);
                        BB.Add(bb);
                    }
                }
                else "".Substring(2);
                return BB;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetBill_BookByBookID", e.ToString());
                return null;
            }
        }

        public List<Bill_Book> GetAllBill_Book()
        {
            DB.SetQuery("SELECT BillID, BookID, Discount, Copy FROM Bill_Book");

            try
            {
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Bill_Book> BB = new List<Bill_Book>();
                Bill_Book bb = new Bill_Book();
                if (llo.Count > 0)
                {
                    foreach (List<object> lo in llo)
                    {
                        bb = new Bill_Book();
                        bb.Bill = (new BillDAL()).GetBillByID(Convert.ToInt16(lo[0]));
                        bb.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[1]));
                        bb.Discount = Convert.ToDouble(lo[2]);
                        bb.Copy = Convert.ToInt16(lo[3]);
                        BB.Add(bb);
                    }
                }
                else "".Substring(2);
                return BB;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetAllBill_Book", e.ToString());
                return null;
            }
        }
    }
}
